package com.xwaf.platform.system.auth.serviceimpl;

import java.util.Date;
import java.util.List;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.plugins.Page;
import com.xwaf.platform.common.api.entity.ELUser;
import com.xwaf.platform.common.service.service.CrudService;
import com.xwaf.platform.common.utils.StringHelper;
import com.xwaf.platform.system.api.entity.auth.User;
import com.xwaf.platform.system.api.service.auth.UserService;
import com.xwaf.platform.system.auth.dao.UserDao;

/**
 * 
 * <p>
 * 用户表
 * <p>
 * 
 * @author 李聪 <br>
 * @email xwaf_love@yeah.net <br>
 * @since JDK 1.8<br>
 * @date 2019年12月27日 下午7:47:37 <br>
 * @see 无<br>
 *      Copyright (c) 2019, xwaf_love@yeah.net All Rights Reserved.<br>
 */
@Transactional(readOnly = true)
@Service("userService")
public class UserServiceImpl extends CrudService<UserDao, User> implements UserService {

	@Autowired
	private UserDao userDao;

	@Override
	public Page<User> selPageList(Page<User> page, User user) {
		List<User> list = userDao.selPageList(page, user);
		for (User u : list) {
			u.setPassword(null);
		}
		page.setRecords(list);
		return page;
	}

	@Override
	public User getUserOne(String userId) {
		User u = userDao.selectById(userId);
		if (StringHelper.isNotEmpty(u)) {
			u.setPassword(null);
			return u;
		}
		return null;
	}

	@Override
	public Set<String> getUserPermissions(String userId) {
		return null;
	}

	@Override
	public User queryByUserName(String username) {
		User user = new User();
		user.setEmail(username);
		User u = userDao.selectOne(user);
		if (StringHelper.isNotEmpty(u)) {
			// u.setPassword(null);
			return u;
		}
		return null;
	}

	@Override
	public boolean updateUserPassword(String newPassword, ELUser user) {
		User up = new User();
		up.setId(user.getId());
		up.setPassword(newPassword);
		up.setUpdateBy(user.getId());
		up.setUpdateTime(new Date());
		return userDao.updateById(up) > 0 ? true : false;
	}

}
